package com.xindaoapp.happypet.view.picture;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.xindaoapp.happypet.R;

public class SelectPicPopupWindow extends PopupWindow {
    /** 拍照 */
    private Button btn_take_photo;
    /** 从相册选择 */
    private Button btn_pick_photo;
    private Button btn_cancel;
    /** 8s视频 */
    private Button btn_take_video;

    /**
     * 显示类型
     * 
     * @author chenghao
     */
    public enum SHOWTYPE {
        /** 显示全部 */
        ALL, /** 视频 */
        VIDEO, /** 图片 */
        PIC
    }

    public SelectPicPopupWindow(Activity context, OnClickListener itemsOnClick, SHOWTYPE isShowVideo) {
        super(context);
        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View mMenuView = inflater.inflate(R.layout.function_list_layout, null);
        // LinearLayout pop_layout = (LinearLayout) mMenuView.findViewById(R.id.pop_layout);
        // Drawable background = pop_layout.getBackground();
        // background.setAlpha(100);
        btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
        btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
        // btn_take_video = (Button) mMenuView.findViewById(R.id.btn_take_video);
        // if (isShowVideo == SHOWTYPE.VIDEO) {
        // btn_take_photo.setVisibility(View.GONE);
        // btn_pick_photo.setVisibility(View.GONE);
        // } else if (isShowVideo == SHOWTYPE.PIC) {
        // btn_take_video.setVisibility(View.GONE);
        // }
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        // 取消按钮
        btn_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 销毁弹出框
                dismiss();
            }
        });
        // 设置按钮监听
        btn_pick_photo.setOnClickListener(itemsOnClick);
        btn_take_photo.setOnClickListener(itemsOnClick);
        // btn_take_video.setOnClickListener(itemsOnClick);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        // 0xb0000000
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.transparent));
        // // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }
}
